<?php
/**
 * Copyright (c) 2006-2007, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

define('UPLOAD_ERR_FILE_EXISTS',       'UPLOAD_ERR_FILE_EXISTS');
define('UPLOAD_ERR_FILE_NOT_UPLOADED', 'UPLOAD_ERR_FILE_NOT_UPLOADED');

class FileUpload
{
	function save($name, $path_to, $override=false)
	{
		# uploaded?
		$ok = $this->exists($name);
		
		# error?
		if ($ok !== UPLOAD_ERR_OK)
			return $ok;
		
		# where to save
		if (is_dir($path_to))
		{
			$_FILES[$name]['name'] = str_replace(array("\\", "'", '"', " "), array('', '-', '-', '_'), $_FILES[$name]['name']);
			$path_to .= DS.$_FILES[$name]['name'];
		}
		
		# overwrite?
		if (file_exists($path_to) and !$override)
			throw new UploadException(UPLOAD_ERR_FILE_EXISTS);
		
		# saves uploaded file
		move_uploaded_file($_FILES[$name]['tmp_name'], $path_to);
		
		return UPLOAD_ERR_OK;
	}
	
	function saveMany($name, $path_to, $override=false)
	{
		$index = 0;
		
		# for each uploaded file
		while(true)
		{
			# uploaded?
			$ok = $this->exists($name, $index=null);
			if ($ok !== true)
				return $ok;
			
			# where to save
			if (is_dir($path_to))
			{
				$_FILES[$name]['name'] = str_replace(array("\\", "'", '"', " "), array('', '-', '-', '_'), $_FILES[$name]['name']);
				$path_to .= DS.$_FILES[$name]['name'];
			}
			
			# overwrite?
			if (file_exists($path_to) and !$override)
				throw new UploadException(UPLOAD_ERR_FILE_EXISTS);
			
			# saves upload file
			move_uploaded_file($_FILES[$name]['tmp_name'][$index], $path_to);
			$index++;
		}
		return UPLOAD_ERR_OK;
	}
	
	function exists($name, $index=null)
	{
		if (!empty($_FILES[$name]))
		{
			if (is_null($index))
			{
				# a single file
				if ($_FILES[$name]['error'] != UPLOAD_ERR_OK)
					throw new UploadException($_FILES[$name]['error']);
				
				if (!is_uploaded_file($_FILES[$name]['tmp_name']))
					throw new UploadException(UPLOAD_ERR_FILE_NOT_UPLOADED);
			}
			else
			{
				# a batch upload (many files)
				if (empty($_FILES[$name]['name'][$index]))
					return false;
				
				if ($_FILES[$name]['error'][$index] != UPLOAD_ERR_OK)
					throw new UploadException($_FILES[$name]['error'][$index]);
				
				if (!is_uploaded_file($_FILES[$name]['tmp_name'][$index]))
					throw new UploadException(UPLOAD_ERR_FILE_NOT_UPLOADED);
			}
			
			# upload exists
			return UPLOAD_ERR_OK;
		}
		
		# no such upload!
		return false;
	}
	
	function getType($name)
	{
		return isset($_FILES[$name]['type']) ? $_FILES[$name]['type'] : null;
	}
	
	function getFilename($name)
	{
		return isset($_FILES[$name]['name']) ? $_FILES[$name]['name'] : null;
	}
	
	function getTmpFilename($name)
	{
		return isset($_FILES[$name]['tmp_name']) ? $_FILES[$name]['tmp_name'] : null;
	}
}

class UploadException extends Exception
{
	function __construct($code)
	{
		switch($code)
		{
			case UPLOAD_ERR_NO_FILE:
				$mesg = _("No file was uploaded.");
				break;
			case UPLOAD_ERR_PARTIAL:
				$mesg = _("The file was only partially uploaded.");
				break;
			case UPLOAD_ERR_INI_SIZE:
				$mesg = sprintf(_("The uploaded file was too big (maximum is %s)."), ini_get('upload_max_filesize'));
				break;
			case UPLOAD_ERR_FORM_SIZE:
				$mesg = sprintf(_("The uploaded file was too big (maximum is %s)."), $_REQUEST['MAX_FILE_SIZE']);
				break;
			case UPLOAD_ERR_NO_TMP_DIR:
				$mesg = _("Server error: Missing a temporary folder.");
				break;
			case UPLOAD_ERR_CANT_WRITE:
				$mesg = _("Server error: unable to save uploaded file.");
				break;
			case UPLOAD_ERR_EXTENSION:
				$mesg = _("File extension refused.");
				break;
			case UPLOAD_ERR_FILE_NOT_UPLOADED:
				$mesg = _("Security alert: this file hasn't been uploaded!");
				break;
			case UPLOAD_ERR_FILE_EXISTS:
				$mesg = _("A file sharing this name already exists.");
				break;
			default:
				$mesg = _("An unknown file upload error occured.");	
		}
		parent::__construct($mesg, (double)$code);
	}
}
?>